﻿using System;
using Microsoft.AspNetCore.Mvc;
using Prayol.SSOClient.WebApi.Common;
using Prayol.SSOClient.WebApi.Models;
using Prayol.SSOClient.WebApi.Services;

namespace Prayol.SSOClient.WebApi.Controllers
{
    /// <inheritdoc />
    /// <summary>
    /// 授权API
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class AuthController : ControllerBase
    {
        private readonly AuthService _authService;

        public AuthController(AuthService authService)
        {
            _authService = authService;
        }

        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Login(PassportLoginRequest request)
        {
            //todo:异常的统一处理
            request.IpAddress = HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString();

            LoginResult loginResult;
            try
            {
                loginResult = _authService.Login(request);
            }
            catch (Exception ex)
            {
                return new JsonResult(new Response
                {
                    Code = 500,
                    Message = ex.Message
                });
                //日志记录
            }
            return new JsonResult(new Response<LoginResult>() { Code = 200, Result = loginResult });
        }

        /// <summary>
        /// 登出
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpGet("LoginOut")]
        public IActionResult LoginOut(string token)
        {
            try
            {
                _authService.LoginOut(token);
                return new JsonResult(new Response {Code = 200});

            }
            catch (Exception ex)
            {
                return new JsonResult(new Response{Code = 500,Message = ex.Message});
            }
        }

        /// <summary>
        /// 检查token是否可用
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpGet("ChekcStatus")]
        public IActionResult CheckStatus(string token)
        {
            return new JsonResult(new Response<bool>(){Code = 200,Result = _authService.CheckToken(token)});
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpGet("GetUser")]
        public IActionResult GetUser(string token)
        {
            return new JsonResult(new Response<UserAuthSession>(){Code = 200,Result = _authService.GetUserInfoByToken(token)});
        }

        /// <summary>
        /// 获取用户信息以及该用户的权限信息
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpGet("GetUserWithccessedControls")]
        public IActionResult GetUserWithccessedControls(string token)
        {
            var userAuthSession = _authService.GetUserInfoByToken(token);
            var result = _authService.GetAccessedControls(userAuthSession.Account);
            return new JsonResult(new Response<UserWithAccessedCtrls>(){Code = 200,Result = result});
        }
    }
}